package wnhz.com.business.shareshop.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.groupshop.ui.PayActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.uitls.dialog.EnterCodeDialog;
import wnhz.com.business.uitls.dialog.SimpleDialog;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 确认支付
 */
public class PayActivityS extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.tv_allprice)
    TextView tv_allprice;
    @BindView(R.id.tv_can_money)
    TextView tv_can_money;
    @BindView(R.id.ll_yue)
    LinearLayout ll_yue;
    @BindView(R.id.ll_wx)
    LinearLayout ll_wx;
    @BindView(R.id.ll_zhifubao)
    LinearLayout ll_zhifubao;
    @BindView(R.id.ll_yinlian)
    LinearLayout ll_yinlian;

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;

    @BindView(R.id.lay_submit)
    TextView lay_submit;
    @BindView(R.id.btn_yue)
    ImageView btn_yue;
    @BindView(R.id.btn_wx)
    ImageView btn_wx;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.btn_zhifubao)
    ImageView btn_zhifubao;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.btn_yinlian)
    ImageView btn_yinlian;
    @BindView(R.id.ll_moreWay)
    LinearLayout llMoreWay;
    @BindView(R.id.lay)
    LinearLayout lay;

    private EnterCodeDialog dialog;
    private SimpleDialog simpleDialog;
    private Context context;
    private String price;
    private String order_no;
    private String order_id;
    private String yue;
    private String from;

    public static Intent createIntent(Context context, String order_no, String price, String from) {
        Intent intent = new Intent(context, PayActivityS.class);
        intent.putExtra("order_no", order_no);
        intent.putExtra("price", price);
        intent.putExtra("from", from);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        price = intent.getStringExtra("price");
        order_no = intent.getStringExtra("order_no");
        order_id = intent.getStringExtra("order_id");
        from = intent.getStringExtra("from");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pays);
        ButterKnife.bind(this);
        parseIntent();
        actionbar.setData("选择支付方式", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

//        initData();
        initView();
    }

    private void initData() {
        SharedPreferences sp = getBaseContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        XUtil.Post(Url.UCENTER_WITHDRAWALSDATA, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("== 支付界面  余额 ==", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }
        });
    }

    private void initView() {
//        lay_submit.setOnClickListener(this);
//        ll_wx.setOnClickListener(this);
//        ll_yinlian.setOnClickListener(this);
//        ll_yue.setOnClickListener(this);
//        ll_zhifubao.setOnClickListener(this);
    }


    //返回键
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
//            if (from.equals("1")) {
//                startActivity(AllOrderActivity.createIntent(PayActivity.this, "2"));
//            }
//            finish();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @OnClick({R.id.ll_yue, R.id.ll_wx, R.id.ll_zhifubao, R.id.ll_yinlian, R.id.lay_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_yue:
                btn_yue.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_selected));
                btn_wx.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_zhifubao.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_yinlian.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));

                break;
            case R.id.ll_wx:
                btn_yue.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_wx.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_selected));
                btn_zhifubao.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_yinlian.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));

                break;
            case R.id.ll_zhifubao:
                btn_yue.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_wx.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_zhifubao.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_selected));
                btn_yinlian.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));

                break;
            case R.id.ll_yinlian:
                btn_yue.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_wx.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_zhifubao.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_unselected));
                btn_yinlian.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_selected));
                break;
            case R.id.lay_submit:
                //支付回调成功后进入下一界面
                startActivity(new Intent(PayActivityS.this, PaySuccessActivityS.class));
                break;


//        case R.id.lay_submit:
//                if (!FastUtils.isFastDoubleClick()) {
//                    if (Prefer.getInstance().getJiaoyi().equals("0")) {
//                        simpleDialog = new SimpleDialog(PayActivity.this, "还没有设置交易密码", "朕知道了", "去设置", new SimpleDialog.OnButtonClick() {
//                            @Override
//                            public void onNegBtnClick() {
//                                simpleDialog.cancel();
//                            }
//
//                            @Override
//                            public void onPosBtnClick() {
//                                startActivity(ForgetWithdrawPwdActivity.createIntent(PayActivity.this, "设置交易密码", Prefer.getInstance().getPhone()));
//                                finish();
//                            }
//                        });
//                        simpleDialog.show();
//                        return;
//                    }
//
//                    if (Double.parseDouble(yue) < Double.parseDouble(price)) {
//                        simpleDialog = new SimpleDialog(PayActivity.this, "余额不足,请充值", "朕知道了", "去充值", new SimpleDialog.OnButtonClick() {
//                            @Override
//                            public void onNegBtnClick() {
//                                simpleDialog.cancel();
//                            }
//
//                            @Override
//                            public void onPosBtnClick() {
//                                startActivity(FundActivity.createIntent(PayActivity.this));
//                                finish();
//                            }
//                        });
//                        simpleDialog.show();
//                        return;
//                    }
//
//                    dialog = new EnterCodeDialog(PayActivity.this, "请输入交易密码", price, new EnterCodeDialog.OnEnterCompleted() {
//                        @Override
//                        public void OnEnterCompleted(String content) {
//                            SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
//                            String token = sp.getString("token", "");
//                            Map<String, Object> map = new HashMap<>();
//                            map.put("token", token);
//                            map.put("order_no", order_no);
//                            map.put("password", Base64Util.encodedString("UNO" + content + "HACHA"));
//
//                            XUtil.Post(Url.PAY_TOPAY, map, new MyCallBack<String>() {
//                                @Override
//                                public void onSuccess(String result) {
//                                    super.onSuccess(result);
//                                    try {
//                                        JSONObject jsonObject = new JSONObject(result);
//                                        LogUtils.e("=======余额支付结果========", result);
//                                        if ("1".equals(jsonObject.optString("re"))) {
//                                            if (from.equals("1")) {
//                                                startActivity(AllOrderActivity.createIntent(PayActivity.this, "2"));
//                                            }
//                                            ToastUtils.showToast(context, "支付成功");
//                                        } else if ("0".equals(jsonObject.optString("re"))) {
//                                            ToastUtils.showToast(context, jsonObject.optString("info"));
//                                            if (from.equals("1")) {
//                                              1  startActivity(OrderDetailsActivity.createIntent(PayActivity.this, order_id));
//                                            }
//                                            finish();
//                                        }
//                                        finish();
//                                    } catch (JSONException e) {
//                                        e.printStackTrace();
//                                    }
//                                }
//
//                                @Override
//                                public void onFinished() {
//                                    super.onFinished();
//                                }
//
//                                @Override
//                                public void onError(Throwable ex, boolean isOnCallback) {
//                                    super.onError(ex, isOnCallback);
//
//                                }
//                            });
//                        }
//                    }, new EnterCodeDialog.OnButtonClick() {
//                        @Override
//                        public void onCancleBtnClick() {
//                            dialog.cancel();
//                        }
//                    });
//                    dialog.show();
//                }
//                break;

        }
    }
}
